package com.itany.corejava.code16_反射;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author MissChen
 * @date 2024年03月26日 16:29
 */
public class Test06_解析注解 {
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
        Class c=Student.class;
        // 1.获取类上的注解
        Annotation[] annotations= c.getAnnotations();
        System.out.println(Arrays.toString(annotations));

        // 2.获取类上的指定的注解
        Annotation annotation=c.getAnnotation(SomeAnnotation.class);
        System.out.println(annotation);

        annotation=c.getAnnotation(OtherAnnotation.class);
        System.out.println(annotation);
        System.out.println("===============");

        //3.获取属性上的所有注解
        // 取当前类中指定的属性,不区分修饰符
        Field nameField=c.getDeclaredField("name");
        Annotation[] annotations2=nameField.getAnnotations();
        System.out.println(Arrays.toString(annotations2));

        // 4.获取属性上的指定注解
        Annotation annotation2=nameField.getAnnotation(SomeAnnotation.class);
        System.out.println(annotation2);

        //5.获取方法上的所有注解
        Method method=c.getDeclaredMethod("study");
        Annotation[] annotations3=method.getAnnotations();
        System.out.println(Arrays.toString(annotations3));

        //6.获取方法上的指定注解
        Annotation someAnnotation=method.getAnnotation(SomeAnnotation.class);
        Annotation otherAnnotation=method.getAnnotation(OtherAnnotation.class);
        System.out.println(someAnnotation);
        System.out.println(otherAnnotation);


        // 7.获取指定注解中指定的属性值
       if(someAnnotation instanceof SomeAnnotation){
            SomeAnnotation someAnnotation2= (SomeAnnotation) someAnnotation;
           // annotation.属性名()
           System.out.println(someAnnotation2.name());
           System.out.println(someAnnotation2.value());
           System.out.println(someAnnotation2.age());
       }














































































































    }
}
